﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Timer;
using Hi_Baidu.ListType;

namespace Hi_Baidu
{
    /// <summary>
    /// Win_ModifyUserInfo.xaml 的交互逻辑
    /// </summary>
    public partial class Win_ModifyUserInfo : Window,IMetaData
    {
        /// <summary>
        ///     路由事件得到的数据库关联字段
        /// </summary>
        private static ArrayList ROUTE_MODIFY_DB_FILED = new ArrayList();
        /// <summary>
        ///     路由事件得到的修改后的新内容
        /// </summary>
        private static ArrayList ROUTE_MODIFY_VISUL_VALUE = new ArrayList();

        public Win_ModifyUserInfo()
        {
            InitializeComponent();
        }

        public Win_ModifyUserInfo(String Key)
        {
            this.key = Key;
            InitializeComponent();
            if(Globle.user.DIC_USERINFO != null)
            {
                this.txt_NickName.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["NICKNAME"]);
                this.txt_Username.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["USERNAME"]);
                this.txt_Name.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["NAME"]);
                this.txt_Prod.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["PROD"]);
                if (BitConverter.ToInt32(Globle.user.DIC_USERINFO["SEX"], 0).ToString() == "0")
                {
                    this.RB_Man.IsChecked = true;
                }
                else
                {
                    this.RB_Woman.IsChecked = true;
                }
                this.txt_Tel.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["TEL"]);
                this.txt_Email.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["EMAL"]);
                this.txt_OrgPassword.Password = Encoding.Default.GetString(Globle.user.DIC_USERINFO["PASSWORD"]);
                this.Img_HeadImage.Source = new BitmapImage(new Uri("pack://application:,,,/syshead/" + Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]) + ".jpg"));

                //TextBox - 路由事件
                this.AddHandler(TextBox.TextChangedEvent, (RoutedEventHandler)OnAllTextBoxChanged, true);
            }
        }

        /// <summary>
        ///     路由事件 - 处理所有TextBox的OnTextChanged事件
        /// </summary>
        public void OnAllTextBoxChanged(Object sender, RoutedEventArgs e)
        {
            bool IS_FOUND = false;
            int Index = 0;
            //获得更改源
            TextBox tb_TempModify = e.Source as TextBox;
            //判断是否关联数据库字段
            if (tb_TempModify.Tag != null)
            {
                //更改的内容与原内容不同
                if (Encoding.Default.GetString(Globle.user.DIC_USERINFO[tb_TempModify.Tag.ToString().ToUpper()]) != tb_TempModify.Text)
                {
                    //查找是否该挂起更改是否已经存在
                    for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == tb_TempModify.Tag.ToString())
                        {
                            IS_FOUND = true;
                            Index = i;
                            break;
                        }
                    }
                    //如果不存在
                    if (!IS_FOUND)
                    {
                        //如果在集合中存在
                        if (Globle.updateUserInfoManager.GetMemberByKey(tb_TempModify.Tag.ToString()) != null)
                        {
                            this.ChanageUpdateUserTypeValue(tb_TempModify.Tag.ToString(), tb_TempModify.Text);
                        }
                        else
                        {
                            this.CreateUpdateUserType(tb_TempModify.Tag.ToString(), tb_TempModify.Text);
                        }
                        ROUTE_MODIFY_DB_FILED.Add(tb_TempModify.Tag.ToString());
                        ROUTE_MODIFY_VISUL_VALUE.Add(tb_TempModify.Text);
                    }
                    else
                    {
                        this.ChanageUpdateUserTypeValue(tb_TempModify.Tag.ToString(), tb_TempModify.Text);
                        //存在
                        ROUTE_MODIFY_VISUL_VALUE[Index] = tb_TempModify.Text;
                    }
                }
                else
                {
                    //查找是否该挂起更改是否已经存在
                    for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == tb_TempModify.Tag.ToString())
                        {
                            Globle.updateUserInfoManager.Remove(tb_TempModify.Tag.ToString());
                            //移除相同的部分
                            ROUTE_MODIFY_DB_FILED.Remove(ROUTE_MODIFY_DB_FILED[i]);
                            ROUTE_MODIFY_VISUL_VALUE.Remove(ROUTE_MODIFY_VISUL_VALUE[i]);
                            break;
                        }
                    }
                }
                this.ChanageApplyButtonState();
            }
        }

        #region IMetaData 成员

        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            ROUTE_MODIFY_DB_FILED.Clear();
            ROUTE_MODIFY_VISUL_VALUE.Clear();
            this.Close();
            //从窗口管理器中移除
            Globle.popWindowManager.Dispose(this.key);
        }

        /// <summary>
        ///     根据用户挂起，更换当前应用按钮可用状态
        /// </summary>
        private void ChanageApplyButtonState()
        {
            if (ROUTE_MODIFY_DB_FILED.Count > 0)
            {
                if (this.btn_Apply != null)
                {
                    this.btn_Apply.IsEnabled = true;
                }
            }
            else
            {
                if (this.btn_Apply != null)
                {
                    this.btn_Apply.IsEnabled = false;
                }
            }
        }

        /// <summary>
        ///     为用户挂起数据创建新的数据类型，
        ///     并且加入到用户挂起集合中
        /// </summary>
        /// <param name="Key" type="string">
        ///     <para>
        ///         唯一标示
        ///     </para>
        /// </param>
        /// <param name="NewValue" type="string">
        ///     <para>
        ///         新值
        ///     </para>
        /// </param>
        public void CreateUpdateUserType(String Key, String NewValue)
        {
            //如果不存在则添加
            if(!Globle.updateUserInfoManager.Equals(Key))
            {
                TYPE_UPDATE_USERINFO TUDI = new TYPE_UPDATE_USERINFO(Key);
                TUDI.New_Value = NewValue;
                Globle.updateUserInfoManager.Add(TUDI);
            }
        }

        /// <summary>
        ///     将指定值更新到已经存在的用户挂起信息集合中
        /// </summary>
        /// <param name="Key" type="string">
        ///     <para>
        ///         唯一标示
        ///     </para>
        /// </param>
        /// <param name="NewValue" type="string">
        ///     <para>
        ///         新值
        ///     </para>
        /// </param>
        public void ChanageUpdateUserTypeValue(String Key, String NewValue)
        {
            IMetaData type_UpdateUserInfo = Globle.updateUserInfoManager.GetMemberByKey(Key);
            if (type_UpdateUserInfo != null)
            {
                ((TYPE_UPDATE_USERINFO)type_UpdateUserInfo).New_Value = NewValue;
            }
        }

        private void btn_Apply_Click(object sender, RoutedEventArgs e)
        {
            this.Submit_UserInfo();
        }

        /// <summary>
        /// 提交用户更改
        /// </summary>
        private void Submit_UserInfo()
        {
            //获取路由更改，通过“更新数据”处理层进行分发更改
            this.btn_Apply.IsEnabled = false;
            if (ROUTE_MODIFY_VISUL_VALUE.Count == ROUTE_MODIFY_DB_FILED.Count)
            {
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    //开始向服务器分发更改
                    UpdateProcessModel.UpdateUserChange(
                        this,
                        Encoding.Default.GetBytes(this.txt_Username.Text),
                        Encoding.Default.GetBytes(ROUTE_MODIFY_DB_FILED[i].ToString()),
                        Encoding.Default.GetBytes(ROUTE_MODIFY_VISUL_VALUE[i].ToString())
                        );
                    //开始为每个更改创建超时器
                    UpdateChanageUserInfo_TimeOutEvent updateChanageUserInfo_TimeOutEvent = new UpdateChanageUserInfo_TimeOutEvent(this,
                        Encoding.Default.GetBytes(this.txt_Username.Text),
                        Encoding.Default.GetBytes(ROUTE_MODIFY_DB_FILED[i].ToString()),
                        Encoding.Default.GetBytes(ROUTE_MODIFY_VISUL_VALUE[i].ToString()),
                        "UpdateChanage-" + ROUTE_MODIFY_DB_FILED[i].ToString(),
                        5000, 2);
                    //安装超时器
                    Globle.timeoutTimerManager.InstallTimer(updateChanageUserInfo_TimeOutEvent);
                }
            }
            else
            {
                throw new Exception("路由事件字段数目不对称!");
            }
        }

        private void RB_Man_Checked(object sender, RoutedEventArgs e)
        {
            bool IS_FOUND = false;
            int Index = 0;
            if (BitConverter.ToInt32(Globle.user.DIC_USERINFO["SEX"], 0).ToString() == "0")
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (this.RB_Man.Tag != null)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == this.RB_Man.Tag.ToString())
                        {
                            Globle.updateUserInfoManager.Remove(this.RB_Man.Tag.ToString());
                            //移除相同的部分
                            ROUTE_MODIFY_DB_FILED.Remove(ROUTE_MODIFY_DB_FILED[i]);
                            ROUTE_MODIFY_VISUL_VALUE.Remove(ROUTE_MODIFY_VISUL_VALUE[i]);
                            break;
                        }
                    }
                }
            }
            else
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (this.RB_Man.Tag != null)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == this.RB_Man.Tag.ToString())
                        {
                            IS_FOUND = true;
                            Index = i;
                            break;
                        }
                    }
                }
                //如果不存在
                if (!IS_FOUND)
                {
                    if (this.RB_Man.Tag != null)
                    {
                        //如果在集合中存在
                        if (Globle.updateUserInfoManager.GetMemberByKey(this.RB_Man.Tag.ToString()) != null)
                        {
                            this.ChanageUpdateUserTypeValue(this.RB_Man.Tag.ToString(), "0");
                        }
                        else
                        {
                            this.CreateUpdateUserType(this.RB_Man.Tag.ToString(), "0");
                        }
                        ROUTE_MODIFY_DB_FILED.Add(this.RB_Man.Tag.ToString());
                        ROUTE_MODIFY_VISUL_VALUE.Add("0");
                    }
                }
                else
                {
                    this.ChanageUpdateUserTypeValue(this.RB_Man.Tag.ToString(), "0");
                    //存在
                    ROUTE_MODIFY_VISUL_VALUE[Index] = "0";
                }
            }
            this.ChanageApplyButtonState();
        }

        private void RB_Woman_Checked(object sender, RoutedEventArgs e)
        {
            bool IS_FOUND = false;
            int Index = 0;
            if (BitConverter.ToInt32(Globle.user.DIC_USERINFO["SEX"], 0).ToString() == "1")
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (this.RB_Woman.Tag != null)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == this.RB_Woman.Tag.ToString())
                        {
                            Globle.updateUserInfoManager.Remove(this.RB_Woman.Tag.ToString());
                            //移除相同的部分
                            ROUTE_MODIFY_DB_FILED.Remove(ROUTE_MODIFY_DB_FILED[i]);
                            ROUTE_MODIFY_VISUL_VALUE.Remove(ROUTE_MODIFY_VISUL_VALUE[i]);
                            break;
                        }
                    }
                }
            }
            else
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (this.RB_Woman.Tag != null)
                    {
                        if (ROUTE_MODIFY_DB_FILED[i].ToString() == this.RB_Woman.Tag.ToString())
                        {
                            IS_FOUND = true;
                            Index = i;
                            break;
                        }
                    }
                }
                //如果不存在
                if (!IS_FOUND)
                {
                    if (this.RB_Woman.Tag != null)
                    {
                        //如果在集合中存在
                        if (Globle.updateUserInfoManager.GetMemberByKey(this.RB_Woman.Tag.ToString()) != null)
                        {
                            this.ChanageUpdateUserTypeValue(this.RB_Woman.Tag.ToString(), "1");
                        }
                        else
                        {
                            this.CreateUpdateUserType(this.RB_Woman.Tag.ToString(), "1");
                        }
                        ROUTE_MODIFY_DB_FILED.Add(this.RB_Woman.Tag.ToString());
                        ROUTE_MODIFY_VISUL_VALUE.Add("1");
                    }
                }
                else
                {
                    this.ChanageUpdateUserTypeValue(this.RB_Woman.Tag.ToString(), "1");
                    //存在
                    ROUTE_MODIFY_VISUL_VALUE[Index] = "1";
                }
            }
            this.ChanageApplyButtonState();
        }

        /// <summary>
        ///     修改用户头像
        /// </summary>
        /// <param name="OrgHeadImage" type="string">
        ///     <para>
        ///         原来用户头像名称
        ///     </para>
        /// </param>
        /// <param name="SelectedHeadImage" type="string">
        ///     <para>
        ///         新用户头像名称
        ///     </para>
        /// </param>
        public void ChanageHeadImage(String OrgHeadImage, String SelectedHeadImage, String SelectedImageType)
        {
            bool IS_FOUND = false;
            int Index = 0;
            //用户变更了原有头像
            if (OrgHeadImage != SelectedHeadImage)
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (ROUTE_MODIFY_DB_FILED[i].ToString() == "HeadImage")
                    {
                        IS_FOUND = true;
                        Index = i;
                        break;
                    }
                }
                //没找到
                if (!IS_FOUND)
                {
                    //如果在集合中存在
                    if (Globle.updateUserInfoManager.GetMemberByKey("HeadImage") != null)
                    {
                        this.ChanageUpdateUserTypeValue("HeadImage", SelectedHeadImage);
                    }
                    else
                    {
                        this.CreateUpdateUserType("HeadImage", SelectedHeadImage);
                    }
                    ROUTE_MODIFY_DB_FILED.Add("HeadImage");
                    ROUTE_MODIFY_VISUL_VALUE.Add(SelectedHeadImage);
                }
                else
                {
                    this.ChanageUpdateUserTypeValue("HeadImage", SelectedHeadImage);
                    ROUTE_MODIFY_VISUL_VALUE[Index] = SelectedHeadImage;
                }
            }
            else
            {
                //查找是否该挂起更改是否已经存在
                for (int i = 0; i < ROUTE_MODIFY_DB_FILED.Count; i++)
                {
                    if (ROUTE_MODIFY_DB_FILED[i].ToString() == "HeadImage")
                    {
                        Globle.updateUserInfoManager.Remove("HeadImage");
                        ROUTE_MODIFY_DB_FILED.Remove(ROUTE_MODIFY_DB_FILED[i]);
                        ROUTE_MODIFY_VISUL_VALUE.Remove(ROUTE_MODIFY_VISUL_VALUE[i]);
                        break;
                    }
                }
            }
            this.Img_HeadImage.Source = new BitmapImage(new Uri("pack://application:,,,/syshead/" + SelectedHeadImage + "." + SelectedImageType));
            this.ChanageApplyButtonState();
        }

        private void btn_Ok_Click(object sender, RoutedEventArgs e)
        {
            if (ROUTE_MODIFY_DB_FILED.Count <= 0 && ROUTE_MODIFY_VISUL_VALUE.Count <= 0)
            {
                Globle.popWindowManager.Dispose("ModifyUserInfo");
                ROUTE_MODIFY_DB_FILED.Clear();
                ROUTE_MODIFY_VISUL_VALUE.Clear();
                this.Close();
            }
            else
            {
                this.Submit_UserInfo();
                Globle.popWindowManager.Dispose("ModifyUserInfo");
                ROUTE_MODIFY_DB_FILED.Clear();
                ROUTE_MODIFY_VISUL_VALUE.Clear();
                this.Close();
            }
        }

        private void btn_ChangeImage_Click(object sender, RoutedEventArgs e)
        {
            //当前窗口集合中未存在该窗口
            if (!Globle.popWindowManager.ActiveWindow("SelectHeadImage"))
            {
                Win_ChanageHeadImage win_ChanageHeadImage = new Win_ChanageHeadImage("SelectHeadImage", this, Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]));
                Globle.popWindowManager.Add(win_ChanageHeadImage);
                win_ChanageHeadImage.Show();
            }
        }
    }
}
